package cn.zkteco.doordu.utils;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2017/11/21.
 */

public class DateUtil {

    /**
     * 日期转时间
     *
     * @return str
     */
    public static String date2string() {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式

        String hehe = dateFormat.format(now);
        System.out.println(hehe);

        Calendar c = Calendar.getInstance();//可以对每个时间域单独修改

        int weekday = c.get(Calendar.DAY_OF_WEEK) - 1;
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int day = c.get(Calendar.DAY_OF_MONTH);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        int second = c.get(Calendar.SECOND);

        String sWeek = "0" + weekday;
        String sMonth, sDay, sHour, sMinute, sSecond, sYear;
        if (month < 10) {
            sMonth = "0" + month;
        } else {
            sMonth = String.valueOf(month);
        }
        if (day < 10) {
            sDay = "0" + day;
        } else {
            sDay = String.valueOf(day);
        }

        if (hour < 10) {
            sHour = "0" + hour;
        } else {
            sHour = String.valueOf(hour);
        }
        if (minute < 10) {
            sMinute = "0" + minute;
        } else {
            sMinute = String.valueOf(minute);
        }
        if (second < 10) {
            sSecond = "0" + second;
        } else {
            sSecond = String.valueOf(second);
        }

        sYear = String.valueOf(year).substring(2);

        String curTime = sSecond + sMinute + sHour + sDay + sWeek + sMonth + sYear;
        return curTime;
    }


    /**
     * byte -> date str
     *
     * @param bytes 16进制字节数组
     * @param radix 2,8,16进制
     * @return string date
     */
    public static String byte2date(byte[] bytes, int radix) {
        String longStr = new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
        long l = Long.parseLong(longStr, 10);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return format.format(new Date(l * 1000L));
    }
}
